//! @Author: DengLibin
//! @Date: Create in 2024-07-17 13:52:46
//! @Description:

use my_trait::{MustHaveFieldType, MustHaveTableName, Struct2Map};
use serde::{Deserialize, Serialize};
use serde_json::Value;


pub mod mysql;

pub mod pg;
pub mod sqlite;

pub mod mysql2sqlite;

pub mod sqlserver;
pub mod sqlserver2sqlite;
pub mod copy_310_data;
pub mod mysql2mysql;

/// @Author: DengLibin
/// @Date: Create in 2024-07-17 15:13:56
/// @Description: 分页数据
#[derive(Debug, Serialize, Deserialize)]
pub struct PageData<T: Serialize> {
    pub page: i32,
    pub size: i32,
    pub total: i64,
    pub data: Vec<T>,
}


/// @Author: DengLibin
/// @Date: Create in 2025-02-07 15:30:22
/// @Description: 获取实体的字段名称,字段类型,对应的字段值
fn field_type_vlues<E>(e: &E) -> (Vec<String>, Vec<String>, Vec<Value>)
where
    E: MustHaveTableName + MustHaveFieldType + Struct2Map,
{
    let mut fields: Vec<String> = vec![];
    //字段的值
    let mut params: Vec<Value> = vec![];
    //字段的类型
    let mut types: Vec<String> = vec![];

    let field_types = E::field_types_map();
    //获取字段类型

    let field_values = e.to_map();
    //要添加的字段
    for (field, value) in field_values {
        params.push(value);
        let field_type = field_types.get(field.as_str());
        if let Some(type_str) = field_type {
            types.push(type_str.to_string()); //该字段类型
        } else {
            types.push("".to_string());
        }
        fields.push(field); //字段名称
    }
    (fields, types, params)
}

///根据字段获取字段的值
fn field__vlues<E>(e: &E, fields: &Vec<String>) -> Vec<Value>
where
    E: MustHaveTableName + MustHaveFieldType + Struct2Map,
{
    //字段的值
    let mut params: Vec<Value> = vec![];
    
    let mut field_values = e.to_map();
    //字段
    for field in fields.iter() {
        let ov = field_values.remove(field);
        if let  Some(v) = ov {
            params.push(v);
        }else{
            params.push(Value::Null);
        }
    }
    params
}